package cn.edu.zut.project1.mapper;

import cn.edu.zut.project1.pojo.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper

public interface ProductMapper {

    /**
     * 1.发布商品(插入数据库)
     */
    @Insert("insert into product(name,detail,cover_list,old_level,category_id,user_id," +
            "inventory,price,is_bargain,create_time,status) values (" +
            "#{name}, #{detail}, #{coverList}, #{oldLevel}, #{categoryId}, #{userId}," +
            "#{inventory}, #{price}, #{isBargain}, now(),0)")
    int insert(Product product);

    // 2. 查询当前用户发布的所有商品（我的商品）
    @Select("select * from product where user_id = #{userId} order by create_time desc")
    List<Product> selectByUserId(Integer userId);

    // 3. 查询单个商品详情（点击商品进入详情页）
    @Select("select * from product where id = #{productId}")
    Product selectById(Integer productId);

    // 4. 修改商品状态（如“已卖出”“下架”）
    @Update("update product set status = #{status} where id = #{productId}")
    int updateStatus(Integer productId, Integer status);

    // 5. 分页查询所有在售商品（首页展示用）
    @Select("select * from product where status = 0 order by create_time desc")
    List<Product> selectAllOnSale();

    // 6. 修改商品信息（只改允许修改的字段：名称、价格、详情、新旧程度、是否支持砍价）
    @Update("update product set " +
            "name = #{name}, " +
            "detail = #{detail}, " +
            "old_level = #{oldLevel}, " +
            "price = #{price}, " +
            "is_bargain = #{isBargain} " +
            "where id = #{id} and user_id = #{userId}") // 加user_id条件，确保只能改自己的
    int updateProduct(Product product);

    // 7. 删除商品（可选，谨慎用，建议先下架再删除）
    @Delete("delete from product where id = #{productId} and user_id = #{userId}")
    int deleteProduct(Integer productId, Integer userId);

    // 8. 根据商品类别展示商品
    @Select("select * from product where category_id = #{categoryId} and status = 0 order by create_time desc")
    List<Product> selectByCategoryId(Integer categoryId);




}
